package fr.utbm.set.safeplatoon.common.merging;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;

import org.arakhne.tinyMAS.core.AgentIdentifier;
import org.arakhne.tinyMAS.core.Kernel;
import org.arakhne.tinyMAS.core.MessageTransportService;
import org.arakhne.tinyMAS.core.YellowPages;
import org.jdom2.JDOMException;

import fr.utbm.set.safeplatoon.common.merging.data.Evaluation;
import fr.utbm.set.safeplatoon.common.merging.data.Lms;
import fr.utbm.set.safeplatoon.common.merging.gui.GUI;
import fr.utbm.set.safeplatoon.common.merging.network.ServerCMD;
import fr.utbm.set.safeplatoon.common.merging.network.ServerLMS;
import fr.utbm.set.safeplatoon.common.merging.sma.agent.Animat;
import fr.utbm.set.safeplatoon.common.merging.sma.agent.StandardAgent;
import fr.utbm.set.safeplatoon.common.merging.sma.environment.WorldModel;
import fr.utbm.set.safeplatoon.common.merging.sma.environment.objet.GoalEntity;
import fr.utbm.set.safeplatoon.common.merging.util.ThreadUtil;



@SuppressWarnings("restriction")
public class Simulation extends
Kernel<Animat, WorldModel, YellowPages, MessageTransportService> {

	public static double WORLD_SIZE_X = 700;
	public static double WORLD_SIZE_Y = 700;
	private WorldModel g;

	private GoalEntity goalTarget;
	private GoalEntity goalEO;

	private  ArrayList<Lms> lms = new ArrayList<Lms>();

	public ArrayList<Lms> getLms() {
		return lms;
	}

	public void setWorld(WorldModel g) {
		this.g = g;
	}

	public WorldModel getWorldModel() {
		return g;
	}

	public void setLms(ArrayList<Lms> lms) {
		this.lms = lms;
	}



	public GoalEntity getGoalTarget() {
		return goalTarget;
	}

	public void setGoalTarget(GoalEntity goalTarget) {
		this.goalTarget = goalTarget;
	}

	public Simulation() {
		super();

	}

	
	/**
	 * Programme Principal
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public void init() throws IOException  {


		
		g = new WorldModel(WORLD_SIZE_X, WORLD_SIZE_Y);
		this.setEnvironment(g);
		
		Lms lms = new Lms(1, 180, 50, g);
		
		GUI gui = new GUI(this);
		gui.setVisible(true);
		Evaluation e = new Evaluation(this, gui);
		ServerLMS serverLMS = new ServerLMS(2003, 180);
		ServerCMD servercmd = new ServerCMD(2002, e);
		
		serverLMS.addLmsListener(lms);
		
		
		ThreadUtil.execute(serverLMS);
		ThreadUtil.execute(servercmd);
		ThreadUtil.execute(e);
		
	}

	public void start(){

		if(goalTarget == null){
			goalTarget = new GoalEntity(true);
			g.addObject(goalTarget);
		}
		
		if(goalEO == null){
			goalEO = new GoalEntity(false);
			g.addObject(goalEO);
		}

		for (int i = 0; i < 100; i++) {
			StandardAgent d = new StandardAgent();
			this.addAgent(d) ;

		}


		this.setWaitingDuration(10);

		this.run();
		// Force quit
		System.exit(0);
	}

	public GoalEntity getGoalEO() {
		return goalEO;
	}

	public void setGoalEO(GoalEntity goalEO) {
		this.goalEO = goalEO;
	}


}